package com.stark.commons.spring.core.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.stark.commons.spring.core.boot.properties.MinioProperties;

import io.minio.MinioClient;

/**
 * MinIO 自动配置。
 * @author Ben
 * @since 1.1.0
 * @version 1.0.0
 */
@Configuration
@ConditionalOnClass(name = "io.minio.MinioClient")
@ConditionalOnProperty(prefix = "spring.minio", name = "endpoint")
@EnableConfigurationProperties(MinioProperties.class)
public class MinioAutoConfig {
	
	@Autowired
	private MinioProperties minioProperties;
	
	@Bean
	@ConditionalOnMissingBean
	public MinioClient minioClient() {
		return MinioClient.builder()
			.endpoint(minioProperties.getEndpoint(), minioProperties.getPort(), minioProperties.isSecure())
			.credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
			.build();
	}

}
